package com.gemme.gemme.wode;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.gemme.gemme.BaseActivity;
import com.gemme.gemme.GeBaseActivity;
import com.gemme.gemme.R;
import com.gemme.gemme.login.Constant;
import com.gemme.gemme.manager.DataManager;
import com.gemme.gemme.view.AddressView;
import com.gemme.gemme.view.BottomDialog;
import com.gemme.gemme.view.SwitchView;
import com.gemme.gemme.view.Topview;
import com.gemme.gemme.view.TvEditText;
import com.gemme.gemme.view.TvLeftTvArrow;
import com.gemme.gemme.view.TvTvArrow;
import com.jaeger.library.StatusBarUtil;

import java.util.HashMap;
import java.util.List;

import retrofit2.http.GET;

public class AddAddressActivity extends BaseActivity {
    String biaoqian = "";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_address);
        StatusBarUtil.setTransparent(this);
        initTop();
        initView();
        getIntentdata();
    }
    String id;
    private void getIntentdata() {
        try {
            Intent intent = getIntent();
            String data = intent.getStringExtra("data");
            AddressBean.DataBean dataBean = JSON.parseObject(data, AddressBean.DataBean.class);

            id = dataBean.getId() == null? "" : dataBean.getId() ;
            shoujianren.setRightText(dataBean.getAddressee());
            phone.setRightText(dataBean.getMobile());
            address.setRightText(dataBean.getProvince() + dataBean.getCity() + dataBean.getCounty());
            xxdz.setRightText(dataBean.getAddress());
            biaoqian = dataBean.getHomeType() + "";
            String type = "请选择";
            if("1".equals(biaoqian) ){
                type = "家";
            }else if("2".equals(biaoqian)){
                type = "公司";
            }else if("3".equals(biaoqian)){
                type = "学校";
            }
            dzbq.setRightText(type);
            int isDefault = dataBean.getIsDefault();
            Boolean isOn = isDefault == 1 ? true : false;
            switchview.setState(isOn);

//            address.setRightText(dataBean);
//            dzbq.setRightText(dataBean);
//            szwmrdz.setRightText(dataBean);
        }catch (Exception e){

        }
    }

    private AddressData addressText = new AddressData();
    TvEditText  shoujianren;
    TvEditText  phone;
    TvEditText  xxdz;
    TvLeftTvArrow address;
    TvTvArrow  dzbq;
    SwitchView switchview;
//    TvTvArrow  szwmrdz;
    AddressView addressView;
    private void initView() {
        addressView = findViewById(R.id.addressView);
        shoujianren = findViewById(R.id.shoujianren);
        phone = findViewById(R.id.phone);
        xxdz = findViewById(R.id.xxdz);
        address = findViewById(R.id.address);
        dzbq = findViewById(R.id.dzbq);
        switchview = findViewById(R.id.switchview);
//        szwmrdz = findViewById(R.id.szwmrdz);

        shoujianren.setLeftText("收件人").setRightHintText("请填写收货人姓名");
        phone.setLeftText("手机号").setRightHintText("请填写收货人手机号");
        xxdz.setLeftText("详细地址").setRightHintText("如道路、门牌号、小区、楼栋号等");
        address.setLeftText("所在地区");
        dzbq.setLeftText("地址标签").setRightText("请选择");
//        szwmrdz.setLeftText("设置为默认地址");

        findViewById(R.id.bt_save).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                toServer();
            }
        });

        address.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                addressView.initData();
            }
        });

        addressView.setOnAddressReturn(new AddressView.OnAddressReturn() {


            @Override
            public void onAddressReturn(AddressData addressText) {
                AddAddressActivity.this.addressText = addressText;
                String addressstr = addressText.getPrivence() + addressText.getCity() + addressText.getXian();
                address.setRightText(addressstr + "");
            }
        });

        dzbq.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showBiaoQianDailog();
            }
        });

    }

    private void showBiaoQianDailog() {
        BottomDialog dialog = new BottomDialog(mContext, R.layout.dialog_biaoqian,
                new int[]{R.id.close,R.id.ll_home, R.id.ll_company,R.id.ll_scholl,R.id.bt_end});

        dialog.show();

        dialog.setOnBottomItemClickListener(new BottomDialog.OnBottomItemClickListener() {
            @Override
            public void onBottomItemClick(BottomDialog dialog, View view) {
                switch (view.getId()) {
                    case R.id.close:
                        dialog.dismiss();
                        break;
                    case R.id.ll_home:
                        biaoqian = "1";
                        dzbq.setRightText("家");
                        dialog.dismiss();
                        break;
                    case R.id.ll_company:
                        biaoqian = "2";
                        dzbq.setRightText("公司");
                        dialog.dismiss();
                        break;
                    case R.id.ll_scholl:
                        biaoqian = "3";
                        dzbq.setRightText("学校");
                        dialog.dismiss();
                        break;
                    case R.id.bt_end:
                        dialog.dismiss();
                        break;
                }
            }
        });

    }

    private void toServer() {
        SharedPreferences MSP = getSharedPreferences(Constant.MSP, Context.MODE_PRIVATE);
        Boolean state = switchview.getState();
        String isDefault = (state == true ? "1" : "0");
//        MSP.edit().putString(Constant.ACCESS_TOKEN, "").apply();
        String userId = MSP.getString(Constant.UserId, "").trim();
        System.out.println("");
        if(TextUtils.isEmpty(userId))
            return;

        String sjrText = shoujianren.getContent();
        String phoneText = phone.getContent();
        String xxdzText = xxdz.getContent();
        
        if(TextUtils.isEmpty(sjrText) ||
                TextUtils.isEmpty(phoneText) ||
                TextUtils.isEmpty(xxdzText) ){
            Toast.makeText(mContext, "收件人/手机号/详细地址不能为空", Toast.LENGTH_SHORT).show();
            return;
        }
/*        http://192.168.1.8:6066/paddress/saveOrUpdate?
        id=-1&province=安徽省1&userId=16&city=合肥市&county=庐阳区
                &address=地址&sort=1&isDefault=0
                &mobile=18856011317&addressee=李金龙&homeType=0&remarks=备注信息*/
       /* String url = DataManager.BaseUrl_jinlong + "paddress/saveOrUpdate"+
                "?id="+ id +
                "&province="+ "安徽省"+
                "&isDefault="+ "0"+
                "&userId="+ userId +
                "&city="+ "合肥"+
                "&county="+ "庐阳区"+
                "&address="+ xxdzText+
                "&sort="+ "1"+
                "&mobile="+ phoneText+
                "&addressee="+sjrText +
                "&homeType="+"0" +
                "&remarks="
                ;*/
        String url = DataManager.BaseUrl_jinlong + "user/paddress/saveOrUpdate"+
                 "?id="+ (id == null ? "" : id)+
                "&province="+ (addressText.getPrivence() == null ? "" : addressText.getPrivence()) +
                "&isDefault="+ isDefault+
                "&userId="+ userId +
                "&city="+ (addressText.getCity() == null ? "" : addressText.getCity()) +
                "&county="+ (addressText.getXian() == null ? "" : addressText.getXian())  +
                "&address="+ xxdzText+
                "&sort="+ "1"+
                "&mobile="+ phoneText+
                "&addressee="+sjrText +
                "&homeType="+ biaoqian +
                "&remarks="
                ;
        HashMap<String, String> headMap = new HashMap<>();
        headMap.put(DataManager.AuthorizationKey,DataManager.getToken(mContext));
        DataManager.toServer(mContext, url, false, "", headMap, new DataManager.GeResponse() {
            @Override
            public void onResponse(String response) {

                try {
                    System.out.println("");
                    finish();
                }catch (Exception e){

                }
            }
        });
    }


    private void initTop() {
//        StatusBarUtil.setColor(this,getResources().getColor(R.color.white_color) );
        StatusBarUtil.setTransparent(this);
//        StatusBarUtil.setTranslucent(this,250);
        Topview topview = findViewById(R.id.topview);
        topview.setContentText("新建地址");
    }


}